<?php
declare (strict_types=1);

namespace app\api\controller;

use think\facade\View;
use think\response\Json;
use app\common\model\News as NewsModel;

class News extends Base
{

    public function index()
    {
        return View::fetch();
    }

    public function info()
    {
        $id = $this->request->param('id');
        $info = NewsModel::where('id', $id)->field('id,create_time,title,content,status,is_del')->find();
        if (empty($info) || $info->status != 1 || $info->is_del) {
            return json(['code' => -1, 'msg' => '记录不存在']);
        }
        unset($info->status);
        unset($info->is_del);
        $url = 'https://' . $_SERVER['SERVER_NAME'] . '/uploads/';
        $info->content = htmlspecialchars_decode(str_replace('/uploads/', $url, $info->content));
        $info->content = str_replace('\\', '', $info->content);


        return json(['code' => 1, 'msg' => 'success', 'result' => $info]);
    }

    public function getList(): Json
    {
        $param = $this->request->param();
        $list = NewsModel::getPageList([], 'id,title,create_time', $param['page'], 20, true);
        return json($list);
    }

}

